﻿var pagecount = "0";
var pagesum = 0;

function cover() {
    if ($(".tg_advertlist").html() == "")
        return false;
    $(".cover").css("display", "block");
    $(".coverdeal").css("display", "block");
    $(".topleft").css("display", "none");
    $(".advetoplefttxt").css("display", "block");
    $(".deleteimg").css("display", "block");
    $("#none").val("1");
}

function show() {
    $(".cover").css("display", "none");
    $(".coverdeal").css("display", "none");
    $(".topleft").css("display", "block");
    $(".advetoplefttxt").css("display", "none");
    $(".deleteimg").css("display", "none");
    $("#none").val("0");
}

$(document).ready(function () {

    if ("wap" == getUrlArgStr("sources")) {
        $(".wamtop").css("display", "none");
    }

    advertlist(page)

    $(window).bind("scroll", function () {
        //当滚动条滚动时
        var scollTop = $(this).scrollTop() + $(this).height();
        var scollH = $(document).height();

        if (scollH - scollTop < 100) {
            if (wait.isShow) { return; }//如果正在加载，则返回
            if (pagesum != 0 && pagesum >= page) { page++; } else { return; }      
            advertlist(page)
        }
        return false;
    });
});

var num = 0;
var page = 1;

function load() {
    page = 1;
    num = 0;
    pagecount = "0";
    $(".tg_advertlist").html("");
    advertlist(page);
}

function advertlist(pages) {
    if (pagecount == "1") { return; }
    if (!islogin()) { window.location.href = 'login.html'; return; }

    if (pagesum != 0&&pagesum < pages) {
        if (pagecount == "0")
            Alert('到底了！');
        pagecount = "1"; return;
    }


    ajax.Getadvertlist("page_index=" + pages, function (data) {
        if (data.code == "4") { Alert(data.msg.toString()); pagecount = "1"; return; }


        pagesum = data.item.pageCount;

        $(".topcententxt").html("广告图片 (" + data.item.itemCount + ")");
        $(data.item.table).each(function () {
            num++;
            var urllink = this.url;
            //            if (urllink.length > 25)
            //                urllink = urllink.substr(0, 25) + "...";
            var titles = this.title;
            if (titles.length > 12)
                titles = titles.substr(0, 12) + "...";
            var float = "";
            if (this.type == 1) { float = "顶部"; } else if (this.type == 2) { float = "底部"; } else { float = "悬浮"; }
            var viewnum = "";
            viewnum = this.view_num == null ? "0" : this.view_num;
            if (userid != this.user_id) {
                $(".tg_advertlist").append("<div><div class=\"advertlist\"><div class=\"coverNo\"></div><div class=\"Recodclick\"> <div style='width: 50px;float: left;'><img src=\"img/flow.jpg\" class=\"Read\" onclick=\"window.location.href='Statistical.html?advertid=" + this.id + "&type=Advert'\" /> " + viewnum + "</div> <div style='width: 50px;float: right;'> <img src=\"img/dianji1.png\" class =\"click\" onclick=\"window.location.href='Statistical.html?advertid=" + this.id + "&type=Advert'\" /> " + this.click + "</div></div><div class=\"advertimg\"><img src=\"img/9.png\" /></div> <div class=\"advertnumber\">" + num + "</div><div class=\"advetname\">" + titles + "</div><div class=\"advetUpload\"><img src=\"" + this.img + "\" class=\"advetUploadsimg\" /> <div class='locationtxt'>" + float + "</div></div><div class=\"advetlink\">  " + urllink + "</div></div><div class=\"NoEditId\"><div class=\"zf\">!</div><div class=\"txt\">通过上级ID获取的图片不允许修改!</div></div> </div>");
            }
            else {
                $(".tg_advertlist").append("<div><div class=\"advertlist\"> <div class=\"coverdeal\"> <img src=\"img/6.png\" style=\"margin-top: 5px; margin-left: 5px;\" /> </div><input type=\"hidden\" name=\"advertid\" value=\"" + this.id + "\"><div class=\"cover\"> </div>  <div class=\"Recodclick\"> <div style='width: 50px;float: left;'><img src=\"img/flow.jpg\" class=\"Read\" onclick=\"window.location.href='Statistical.html?advertid=" + this.id + "&type=Advert'\" /> " + viewnum + "</div> <div style='width: 50px;float: right;'> <img src=\"img/dianji1.png\" class =\"click\" onclick=\"window.location.href='Statistical.html?advertid=" + this.id + "&type=Advert'\" /> " + this.click + "</div> </div><div class=\"advertimg\"><img src=\"img/9.png\" /></div> <div class=\"advertnumber\">" + num + "</div><div class=\"advetname\">" + titles + "</div><div class=\"advetUpload\"><img src=\"" + this.img + "\" class=\"advetUploadsimg\" onclick=\"window.location.href='EditAdvertisting.html?Advertid=" + this.id + "'\" /> <div class='locationtxt'>" + float + "</div></div><div class=\"advetlink\">  " + urllink + "</div></div> </div>");
            }

        });
        $(".coverdeal").off();
        $(".coverdeal").bind("click", function () {

            var barg = $(this).css("background-color").toString();

            if ("rgb(255, 255, 255)" == barg.toString()) {

                $(this).css("background-color", "#F37B0B");
            }
            else {

                $(this).css("background-color", "white");
            }
        });
        

        if ($("#none").val() == "1") {
            cover();
        }


    });
}

function Deteleadvert() {
    if (!islogin()) { window.location.href = 'login.html'; return; }
    var hid = "";
    $(".coverdeal").each(function () {
        if ($(this).css("background-color") != "rgb(255, 255, 255)") {
            hid += $(this).next().val() + ",";
        }
    });
    hid = hid.substring(0, hid.length - 1);
    ajax.Removeadvert("id=1&ids=" + hid, function (data) {
        if (data.msg.toString() == "成功") {
            Alert("成功删除！");
            show();
            load();
        }
    });

}
    